from django.db import models

# Create your models here.

from utils.ModelBaseField import ModelBaseField


class Tag(ModelBaseField):
    """
    标签模型
    """
    name = models.CharField(verbose_name="标签名称", max_length=100, null=False, unique=True, help_text="标签名称")

    class Meta:
        db_table = "tb_tag"  # database中表名
        ordering = ["-update_time", "-id"]  # 排序
        verbose_name = "标签"  # Admin站点中显示的名称
        verbose_name_plural = verbose_name  # Admin站点中显示的名称复数形式

    def __str__(self):
        return '[Tag<{}:{}>]'.format(self.id, self.name)


class Article(ModelBaseField):
    """
    文章模型
    """
    title = models.CharField(verbose_name="标题", max_length=100, null=False, help_text="标题")
    digest = models.CharField(verbose_name="摘要", max_length=300, help_text="摘要")
    content = models.TextField(verbose_name="内容", help_text="摘要")
    clicks = models.IntegerField(verbose_name="点击量", default=0, help_text="点击量")
    thumbnail = models.URLField(verbose_name="缩略图", help_text="缩略图", null=True)
    author = models.ForeignKey("user.User", on_delete=models.SET_NULL, null=True, related_name="articles",
                               verbose_name="作者")
    tag = models.ForeignKey('Tag', on_delete=models.SET_NULL, null=True, verbose_name="所属标签", related_name="articles")

    class Meta:
        db_table = "tb_article"
        ordering = ["-update_time", "-id"]
        verbose_name = "文章"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '[Article<{}:{}>]'.format(self.id, self.title)


class Comment(ModelBaseField):
    """
    评论模型
    """
    content = models.TextField(verbose_name="评论内容", help_text="评论内容")
    author = models.ForeignKey("user.User", on_delete=models.SET_NULL, related_name='author', null=True)
    news = models.ForeignKey("Article", on_delete=models.CASCADE, related_name="article", )
    parent = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True,
                               related_name="son_comments")

    class Meta:
        db_table = "tb_comment"
        ordering = ["-create_time", "-id"]
        verbose_name = "文章评论"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '[Comment<{}:{}>]'.format(self.author.username, self.content)

    def to_dict_data(self):
        """用于序列化"""
        comment_dict = {
            'news_id': self.news.id,
            'id': self.id,
            'content': self.content,
            'author': self.author.username,
            'update_time': self.update_time.strftime('%Y年%m月%d %H:%M'),
        }
        if self.parent:
            #只有一级评论时候不存在parent
            comment_dict['parent'] = {
                'parent_id': self.parent_id,
                'author': self.parent.author.username,
                'content': self.parent.content
            }
        return comment_dict


class HotArticle(ModelBaseField):
    """
    热门文章模型
    """
    PRI_CHOICE = [
        (1, "第一级"),
        (2, "第二级"),
        (3, "第三级"),
    ]

    article = models.OneToOneField("Article", on_delete=models.CASCADE)
    priority = models.IntegerField(verbose_name="优先级", help_text="优先级", choices=PRI_CHOICE)

    class Meta:
        db_table = "tb_hot_article"
        ordering = ['-priority', '-update_time']

    def __str__(self):
        return '[HotArticle<{}:{}>]'.format(self.id, self.article.title)


class Banner(ModelBaseField):
    """
    轮播图
    """

    PRI_CHOICE = [
        (1, "第一级"),
        (2, "第二级"),
        (3, "第三级"),
        (4, "第四级"),
    ]

    img_url = models.URLField(verbose_name="轮播图url", null=False, help_text="轮播图url")
    link_to = models.URLField(verbose_name="链接", default="javascript:void(0);", help_text="链接")
    priority = models.IntegerField(verbose_name="优先级", null=False, help_text="优先级", choices=PRI_CHOICE)

    class Meta:
        db_table = "tb_banner"
        ordering = ["-update_time", "-id"]
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '[Banner<{}:{}>]'.format(self.id, self.img_url)
